﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TTGameEngine;
using UnityEngine.UI;

public enum OptionID {
    OptionID_None = -1,
    OptionID_Talk,
    OptionID_Search,
    OptionID_Fight,
}

[System.Serializable]
public class UIMainData: UIDataBase {
    public class OptionIDConfig {
        public OptionID optionID;
        public string name;
        public string triggerFunc;
    }

    public List<OptionID> optBtns = new List<OptionID>();
    //public OptionIDConfig[] optBtnNames;
    //public string[] optNames;

    public void InitWithOptions(OptionID[] options) {
        optBtns.Clear();
        optBtns.AddRange(options);
    }
}

public class UIMain : UIBase {

    UIMainData m_uidata = null;

    [Tooltip("从下往上，Btn1、Btn2、Btn3...")]
    public Button[] allBtns;
    public Text[] allTexts;

    public override void init() {
        m_uidata = (UIMainData)m_uidatabase;

        for (int i = 0; i < allBtns.Length; i++) {
            Button btn = allBtns[i];
            int btnidx = i;
            btn.onClick.AddListener(() => { OnClick_btn(btnidx); });
        }

        updateOptionBtns();
    }

    public void OnClick_btn(int btnidx)
    {
        Button btn = allBtns[btnidx];
        OptionID opID = m_uidata.optBtns[btnidx];
        DebugUtil.LogFormat("[UIMain]Click Btn [{0}:{1} {2}]", btnidx, btn.name, opID.ToString());
        switch (opID) {
            case OptionID.OptionID_Fight:
                {
                    //mark:todo 需要一个fightID
                    int fightID = 0;
                    UIHelper.UIFloatMsg(string.Format("Test:进入战斗. 战斗ID[{0}]", fightID));
                    TTEventSystem.Ins.RaiseEvent(GameEventID.GE_EnterBattle, fightID);
                    UIHelper.UIFloatMsg(string.Format("Test:退出战斗. 战斗ID[{0}]", fightID));
                    TTEventSystem.Ins.RaiseEvent(GameEventID.GE_ExitBattle, fightID);
                }
                break;
            case OptionID.OptionID_Search:

                break;
            case OptionID.OptionID_Talk:

                break;
            default:

                break;
        }
    }

    public void AddOptionBtn(OptionID optionID) {
        m_uidata.optBtns.Add(optionID);
        updateOptionBtns();
    }

    public void RemoveOptionBtn(OptionID optionID) {
        if(m_uidata.optBtns.Remove(optionID)) {
            updateOptionBtns();
        }
    }

    string GetOptionName(OptionID opID) {
//         string name = m_uidata.optBtnNames[(int)opID].name;
//         return name;
        //return m_uidata.optNames[(int)opID];
        return GameConfig.Ins.OptionBtnNames[(int)opID];
    }

    void updateOptionBtns() {
        for(int i = 0; i < m_uidata.optBtns.Count && i < allBtns.Length; i++) {
            OptionID opID = m_uidata.optBtns[i];
            allBtns[i].SetActive(true);
            allTexts[i].text = GetOptionName(opID);
        }
        for(int i = m_uidata.optBtns.Count; i < allBtns.Length; i++) {
            allBtns[i].SetActive(false);
        }
    }

}
